package com.sgb.kjwl.view.ui.activity.self.userInfo;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityBankInfoBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.BankInfoEntity;
import com.sgb.kjwl.viewmodel.self.userInfo.BankInfoModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

public class BankInfoActivity extends AppActivity {
    private ActivityBankInfoBinding binding;
    @Inject
    BankInfoModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, BankInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(BankInfoActivity.this, R.layout.activity_bank_info);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        viewModel.getBankInfo();

        binding.include.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.include.titleView.setText("开票信息");

        binding.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveValidation();
            }
        });

    }

    private void saveValidation() {
        if (TextUtils.isEmpty(binding.etPaperTitle.getText())) {
            MToast.showToast(BankInfoActivity.this, "发票抬头不能为空");
            return;
        }

        if (TextUtils.isEmpty(binding.etDueNumber.getText())) {
            MToast.showToast(BankInfoActivity.this, "纳税人识别号不能为空");
            return;
        }

        if (TextUtils.isEmpty(binding.etRegisterAddress.getText())) {
            MToast.showToast(BankInfoActivity.this, "注册地址不能为空");
            return;
        }

        if (TextUtils.isEmpty(binding.etMobile.getText())) {
            MToast.showToast(BankInfoActivity.this, "电话不能为空");
            return;
        }

        if (!CheckUtils.isMobileNum(binding.etMobile.getText().toString())) {
            MToast.showToast(BankInfoActivity.this, "请输入正确手机格式");
            return;
        }

        if (TextUtils.isEmpty(binding.etBank.getText())) {
            MToast.showToast(BankInfoActivity.this, "开户行不能为空");
            return;
        }

        if (TextUtils.isEmpty(binding.etBankAccount.getText())) {
            MToast.showToast(BankInfoActivity.this, "银行账号不能为空");
            return;
        }

        /*if (!CheckUtils.isMobileNum(binding.etMobile.getText().toString())) {
            MToast.showToast(BankInfoActivity.this, "请输入正确手机格式");
            return;
        }*/

        viewModel.saveModel(binding.etPaperTitle.getText().toString(), binding.etDueNumber.getText().toString(),
                binding.etRegisterAddress.getText().toString(), binding.etMobile.getText().toString(), binding.etBank.getText().toString(), binding.etBankAccount.getText().toString());
    }

    public void showBankInfo(BankInfoEntity bankInfoEntity) {
        binding.etPaperTitle.setText(bankInfoEntity.getTitle());
        binding.etDueNumber.setText(bankInfoEntity.getTaxNo());
        binding.etRegisterAddress.setText(bankInfoEntity.getAddress());
        binding.etMobile.setText(bankInfoEntity.getMobile());
        binding.etBank.setText(bankInfoEntity.getBank());
        binding.etBankAccount.setText(bankInfoEntity.getBankAccount());
    }

    @Override
    public void success(){
        EventBus.getDefault().postSticky("REFRESH_COMPANY_INFO");
        finish();
    }
}
